#!/bin/bash
set -e

function info() {
echo Usage: `basename $0` in.vcf 
exit 65
}

while getopts  ":b:p:" opts
do
        case  $opts  in
		b) build=$OPTARG;;
		p) out_prefix=$OPTARG;;
		*) info;;
        esac
done
shift $(($OPTIND - 1))


if [ $# -lt 1 ]; then info; fi


. /mnt/ilustre/app/medical/tools/.var

# export PATH=$PATH:/mnt/ilustre/app/medical/tools/impact

# collect_dinuc.pl $1 $data_path/impact/genetic_code.txt | single_remove_synon.pl -| remove_over.01.one.pl -| remove_over.01.two.pl -| get_deleterious.pl - > $out_prefix.deleterious.txt

out_prefix=`basename $1 .vcf`

# hostname

# which python && exit 

# for version compatibility 2.6 2.7

${tools_path}/Python-2.7.10/python $tools_path/script/impact_anno.py $1 $data_path/impact

out_html=${out_prefix}.drugs-prediction.html

# cut -f1-7,9,10 $out_prefix.deleterious.txt > $out_prefix.deleterious.sub.txt

cut -f1-2,4-7,9-10 $out_prefix.af.deleterious.txt|sed '1iChrom\tGRCh37 Pos\tREF\tALT\tFunc.refGene\tGene\tExonicFunc.refGene\tAAChange.refGene' > $out_prefix.deleterious.sub.txt

cat $out_prefix.af.deleterious.txt |awk '{print $NF}'|sed '1iaf' >  $out_prefix.impact.af.txt

paste $out_prefix.deleterious.sub.txt $out_prefix.impact.af.txt |tab2xls.pl - $out_prefix.deleterious.sub.xls

format_jxl.sh $out_prefix.deleterious.sub.xls $out_prefix.deleterious.xls

# if test ! -d impact; then mkdir impact; fi;
# cd impact
# drug_prediction.pl ../$out_prefix.deleterious.txt && mv drug_prediction.txt ../$out_prefix.drug_prediction.txt && mv drug_prediction.html ..

# drugs.py ../$out_prefix.deleterious.txt $data_path/impact &&

# cd ..

# mv *drugs-prediction.txt $out_prefix.drug_prediction.txt

# cat drug_prediction.html |grep -n "LEVEL 1" ~ 7

num_start=`cat $out_html |grep -n 'IMPACT Drug Prediction Analysis' | cut -d: -f1`

num_end=`cat $out_html |grep -in '/body' |cut -d: -f1`

((num_end=$num_end-1))

if test $num_end -eq -1; then num_end=$; fi


cat $out_html |sed -n "${num_start},${num_end}p"|cat $tools_path/html/header.html - $tools_path/html/footer.html > $out_prefix.impact.drugs-prediction.html

# rm -f drugs-prediction.html

. $cmd_done